package iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.adapter;

import static android.content.Context.MODE_PRIVATE;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.R;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.activity.secondActivity.ActivityAddProductImg;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.entity.Product;

public class ConsumerProductAdapter extends ArrayAdapter<Product> {
    TextView productName;
    ImageView productImage;
    TextView productDetails;
    TextView addLink;

    public ConsumerProductAdapter(@NonNull Context context, @NonNull List<Product> products) {
        super(context, 0, products);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_consumer_product_management, parent, false);
        }

        Product product = getItem(position);

        // 获取视图中的控件
        productName = convertView.findViewById(R.id.product_name);
        productImage = convertView.findViewById(R.id.product_image);
        productDetails = convertView.findViewById(R.id.product_details);
        addLink = convertView.findViewById(R.id.add_link);

        // 设置产品名称和生产日期
        productName.setText(product.getProductName());

        // 加载产品图片
        if (product.getImage() != null) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(product.getImage(), 0, product.getImage().length);
            productImage.setImageBitmap(bitmap);
        } else {
            productImage.setImageResource(R.drawable.default_image); // 请替换为您默认的图片资源
        }

        // 设置产品详细信息
        String details = String.format("ID: %s\n分类: %s\n生产日期：%s\n价格: %.2f\n",
                product.getProductId(),
                product.getCategory(),
                product.getProduceDate(),
                product.getPrice());

        // 为产品详情设置点击事件，弹出对话框
        productDetails.setOnClickListener(v -> showProductDetailsDialog(product, details));

        // 设置修改超链接的点击事件
        if ((product.getState() & 1) == 0){
            addLink.setText("为商品拍照");
            addLink.setOnClickListener(v ->setAddLink_1(product));
        }else{
            addLink.setText("信息已完善");
            addLink.setTextColor(Color.BLACK);
        }

        return convertView;
    }

    // 跳转到添加图片页面
    private void setAddLink_1(Product product){
        SharedPreferences sharedPreferences = getContext().getSharedPreferences("ACCOUNT_REMEMBER", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("productId",product.getProductId());
        editor.putString("productName",product.getProductName());
        editor.putString("category",product.getCategory());
        editor.putString("productPrice",product.getPrice().toString());
        editor.apply();
        // 跳转到添加照片页面
        Intent intent = new Intent(getContext(),ActivityAddProductImg.class);
        getContext().startActivity(intent);
        Toast.makeText(getContext(), "补充信息 " + product.getProductName(), Toast.LENGTH_SHORT).show();
    }

    // 无法再添加图片
    private void setAddLink_2(Product product){
        // 创建并显示包含产品详细信息的对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle("信息");
        builder.setMessage("产品名称:"+product.getProductName()+"\n该产品信息已完善勿需补充");
        builder.setPositiveButton("确定", null);
        builder.show();
    }

    private void showProductDetailsDialog(Product product, String details) {
        // 创建并显示包含产品详细信息的对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle("产品详细信息");
        builder.setMessage(details);
        builder.setPositiveButton("确定", null);
        builder.show();
    }
}